package com.tengju.user.infrastructure.repository.star;

import com.tengju.user.domain.model.star.StarUserFeature;
import com.tengju.user.domain.model.star.StarUserFeatureRepository;
import com.tengju.user.domain.model.user.StarLevelEnum;
import com.tengju.user.domain.model.user.UserIdCode;
import com.tengju.user.infrastructure.persistence.mybatis.dataobject.StarUserFeatureDO;
import com.tengju.user.infrastructure.persistence.mybatis.mapper.dealer.custom.StarUserFeatureDOCustomMapper;
import com.tengju.user.infrastructure.repository.star.convert.StarFeatureConvert;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chenhang
 * @Date 2021/1/21
 */
@Repository
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
public class StarUserFeatureRepositoryImpl implements StarUserFeatureRepository {

    private final StarUserFeatureDOCustomMapper starUserFeatureDOCustomMapper;

    @Override
    public void insertOrUpdateStarLevel(UserIdCode idCode, StarLevelEnum starLevel, String updateBy) {
        if (idCode == null || starLevel == null) {
            return;
        }
        starUserFeatureDOCustomMapper.insertOrUpdateStarLevel(idCode.getValue(),starLevel.getCode(),updateBy);
    }

    @Override
    public void insertOrUpdateInviteStarLevel(UserIdCode idCode, StarLevelEnum inviteStarLevel, String updateBy) {
        if (idCode == null || inviteStarLevel == null) {
            return;
        }
        starUserFeatureDOCustomMapper.insertOrUpdateInviteStarLevel(idCode.getValue(),inviteStarLevel.getCode(),updateBy);
    }

    @Override
    public List<StarUserFeature> listAllStarUserFeature() {
        List<StarUserFeatureDO> starUserFeatureDOList = starUserFeatureDOCustomMapper.listAll();
        if (CollectionUtils.isEmpty(starUserFeatureDOList)) {
            return Collections.emptyList();
        }
        return starUserFeatureDOList.stream()
                .map(StarFeatureConvert::convertToModel)
                .collect(Collectors.toList());
    }


}
